package co.yixiang.mall.biz.util;

import co.yixiang.common.core.utils.IpUtil;
import co.yixiang.common.security.utils.SecurityUtils;
import co.yixiang.mall.common.mall.domain.YxUser;
import co.yixiang.upms.common.userLog.domain.YxUserLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;

@Log4j2
@Component
public class YxUserLogUtil {


    /*@Autowired
    private static YxUserLogService yxUserLogService;
*/
    public static Boolean addUserLog(YxUser yxUser,YxUser yxUser1) {



        YxUserLog yxUserLog = new YxUserLog();
        Date date = new Date();
        //字段没有更新返回不作处理
        String fieldLogo = UserLogEqual(yxUser, yxUser1);
        if("".equals(fieldLogo) || fieldLogo == null) {
            return false;
        }
        //添加用用户操作日志
        yxUserLog.setOriginalData(yxUser.toString());
        yxUserLog.setNewData(yxUser1.toString());
        yxUserLog.setUid(yxUser.getUid());
        yxUserLog.setFieldLogo(fieldLogo);
        yxUserLog.setModifyUser(SecurityUtils.getUsername());
        yxUserLog.setRequestIp(IpUtil.getRequestIp());
        yxUserLog.setAddress(IpUtil.getMacId());
        yxUserLog.setUpdateTime(new Timestamp(date.getTime()));
        yxUserLog.setModifyReason(yxUser1.getModifyReason());
        /*yxUserLogService.create(yxUserLog);*/

        return true;

    }

    public static String UserLogEqual(YxUser yxUser,YxUser yxUser2){

        StringBuffer stringBuffer = new StringBuffer();
        if(yxUser.getNickname() !=null && !yxUser.getNickname().equals(yxUser2.getNickname())){
            stringBuffer.append("nickname,");
        }

        if(yxUser.getMark() !=null && !yxUser.getMark().equals(yxUser2.getMark())){
            stringBuffer.append("mark,");
        }

        if(yxUser.getPhone() !=null && !yxUser.getPhone().equals(yxUser2.getPhone())){
            stringBuffer.append("phone,");
        }

        if(yxUser.getRealName() !=null && !yxUser.getRealName().equals(yxUser2.getRealName())){
            stringBuffer.append("realName,");
        }

        if(yxUser.getIntegral() !=null && yxUser.getIntegral().compareTo(yxUser2.getIntegral())!=0){
            stringBuffer.append("integral,");
        }

        if(yxUser.getAdminid() !=null && !yxUser.getAdminid().equals(yxUser2.getAdminid())){
            stringBuffer.append("adminid,");
        }

        return stringBuffer.length()>0?stringBuffer.toString().substring(0,stringBuffer.toString().length()-1):"";
    }

}
